package com.xaw4.applejack.screens;

import javax.microedition.khronos.opengles.GL10;

import com.badlogic.gamedev.tools.GameActivity;
import com.xaw4.applejack.Renderer;
import com.xaw4.applejack.simulation.Simulation;

public class GameLoop implements GameScreen
{
	public Simulation simulation;
	Renderer renderer;	
//	SoundManager soundManager;

	public GameLoop( GL10 gl, GameActivity activity )
	{
		simulation = new Simulation();
		renderer = new Renderer( gl, activity, simulation.getPlayfield());
//		soundManager = new SoundManager( activity );//TOD
	}
	
	public GameLoop(GL10 gl, GameActivity activity, Simulation simulation) 
	{
		this.simulation = simulation;
		
		renderer = new Renderer( gl, activity, simulation.getPlayfield());
//		soundManager = new SoundManager( activity );//TODO
	}

	@Override
	public void render(GL10 gl, GameActivity activity) 
	{	
		renderer.render( gl, activity, simulation);
	}

	@Override
	public void update(GameActivity activity) 
	{	
		processInput( activity );
		simulation.update( activity.getDeltaTime() );
	}
	
	private void processInput( GameActivity activity )
	{		
		if(activity.isTouched()){
			simulation.moveCart(activity.getTouchX()/(float)activity.getViewportWidth());
		}
		
//		if( activity.getAccelerationOnYAxis() < 0 )
//			simulation.moveShipLeft( activity.getDeltaTime(), Math.abs(activity.getAccelerationOnYAxis()) / 10 );
//		else
//			simulation.moveShipRight( activity.getDeltaTime(), Math.abs(activity.getAccelerationOnYAxis()) / 10 );
//	
//		
//		if( activity.isTouched() )
//			simulation.shot();
	}

	public boolean isDone( )
	{
		//TODO
		return simulation.drops > 10;
	}
	
	public void dispose( )
	{
		renderer.dispose();
//		soundManager.dispose();////TODO
	}


}
